package cn.iocoder.yudao.module.member.controller.admin.scorerecord;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.member.controller.admin.scorerecord.vo.*;
import cn.iocoder.yudao.module.member.convert.scorerecord.ScoreRecordConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.scorerecord.ScoreRecordDO;
import cn.iocoder.yudao.module.member.service.scorerecord.ScoreRecordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 用户积分兑换")
@RestController
@RequestMapping("/member/score-record")
@Validated
public class ScoreRecordController {

    @Resource
    private ScoreRecordService scoreRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建用户积分兑换")
    @PreAuthorize("@ss.hasPermission('member:score-record:create')")
    public CommonResult<Long> createScoreRecord(@Valid @RequestBody ScoreRecordCreateReqVO createReqVO) {
        return success(scoreRecordService.createScoreRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新用户积分兑换")
    @PreAuthorize("@ss.hasPermission('member:score-record:update')")
    public CommonResult<Boolean> updateScoreRecord(@Valid @RequestBody ScoreRecordUpdateReqVO updateReqVO) {
        scoreRecordService.updateScoreRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除用户积分兑换")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('member:score-record:delete')")
    public CommonResult<Boolean> deleteScoreRecord(@RequestParam("id") Long id) {
        scoreRecordService.deleteScoreRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得用户积分兑换")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('member:score-record:query')")
    public CommonResult<ScoreRecordRespVO> getScoreRecord(@RequestParam("id") Long id) {
        ScoreRecordDO scoreRecord = scoreRecordService.getScoreRecord(id);
        return success(ScoreRecordConvert.INSTANCE.convert(scoreRecord));
    }

    @GetMapping("/list")
    @Operation(summary = "获得用户积分兑换列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('member:score-record:query')")
    public CommonResult<List<ScoreRecordRespVO>> getScoreRecordList(@RequestParam("ids") Collection<Long> ids) {
        List<ScoreRecordDO> list = scoreRecordService.getScoreRecordList(ids);
        return success(ScoreRecordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得用户积分兑换分页")
    @PreAuthorize("@ss.hasPermission('member:score-record:query')")
    public CommonResult<PageResult<ScoreRecordRespVO>> getScoreRecordPage(@Valid ScoreRecordPageReqVO pageVO) {
        PageResult<ScoreRecordDO> pageResult = scoreRecordService.getScoreRecordPage(pageVO);
        return success(ScoreRecordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出用户积分兑换 Excel")
    @PreAuthorize("@ss.hasPermission('member:score-record:export')")
    @OperateLog(type = EXPORT)
    public void exportScoreRecordExcel(@Valid ScoreRecordExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ScoreRecordDO> list = scoreRecordService.getScoreRecordList(exportReqVO);
        // 导出 Excel
        List<ScoreRecordExcelVO> datas = ScoreRecordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "用户积分兑换.xls", "数据", ScoreRecordExcelVO.class, datas);
    }

}
